package component.medical;

import java.sql.*;
import java.util.Scanner;
import util.ConnectionUtils;

public class MedicalRecord {
    public static void viewMedicalRecords(String patientId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Scanner scanner = new Scanner(System.in);

        try {
            conn = ConnectionUtils.getConn();
            
            while(true) {
                System.out.println("\n=== 就医记录查询 ===");
                System.out.println("1. 查看所有就医记录");
                System.out.println("2. 查看最近一次就医记录");
                System.out.println("3. 按时间范围查询");
                System.out.println("4. 返回上级菜单");
                System.out.print("请选择操作(1-4)：");
                
                String choice = scanner.nextLine();
                
                switch(choice) {
                    case "1":
                        showAllRecords(conn, patientId);
                        break;
                    case "2":
                        showLatestRecord(conn, patientId);
                        break;
                    case "3":
                        showRecordsByDateRange(conn, patientId);
                        break;
                    case "4":
                        return;
                    default:
                        System.out.println("输入有误，请重新选择");
                }
            }
        } catch (SQLException e) {
            System.err.println("查询就医记录时发生错误: " + e.getMessage());
        } finally {
            ConnectionUtils.close(conn, pstmt, rs);
        }
    }
    
    private static void showAllRecords(Connection conn, String patientId) throws SQLException {
        String sql = "SELECT m.id, m.create_time, m.diagnosis, m.prescription, " +
                    "u.name as doctor_name, d.department, d.doctorRank " +
                    "FROM medical_record m " +
                    "JOIN doctor d ON m.doctor_id = d.id " +
                    "JOIN user u ON d.user_id = u.id " +
                    "WHERE m.patient_id = ? " +
                    "ORDER BY m.create_time DESC";
                    
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, patientId);
        ResultSet rs = pstmt.executeQuery();
        
        displayRecords(rs);
        
        rs.close();
        pstmt.close();
    }
    
    private static void showLatestRecord(Connection conn, String patientId) throws SQLException {
        String sql = "SELECT m.id, m.create_time, m.diagnosis, m.prescription, " +
                    "u.name as doctor_name, d.department, d.doctorRank " +
                    "FROM medical_record m " +
                    "JOIN doctor d ON m.doctor_id = d.id " +
                    "JOIN user u ON d.user_id = u.id " +
                    "WHERE m.patient_id = ? " +
                    "ORDER BY m.create_time DESC LIMIT 1";
                    
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, patientId);
        ResultSet rs = pstmt.executeQuery();
        
        displayRecords(rs);
        
        rs.close();
        pstmt.close();
    }
    
    private static void showRecordsByDateRange(Connection conn, String patientId) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入起始日期 (格式：YYYY-MM-DD)：");
        String startDate = scanner.nextLine();
        System.out.println("请输入结束日期 (格式：YYYY-MM-DD)：");
        String endDate = scanner.nextLine();
        
        String sql = "SELECT m.id, m.create_time, m.diagnosis, m.prescription, " +
                    "u.name as doctor_name, d.department, d.doctorRank " +
                    "FROM medical_record m " +
                    "JOIN doctor d ON m.doctor_id = d.id " +
                    "JOIN user u ON d.user_id = u.id " +
                    "WHERE m.patient_id = ? " +
                    "AND DATE(m.create_time) BETWEEN ? AND ? " +
                    "ORDER BY m.create_time DESC";
                    
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, patientId);
        pstmt.setString(2, startDate);
        pstmt.setString(3, endDate);
        ResultSet rs = pstmt.executeQuery();
        
        displayRecords(rs);
        
        rs.close();
        pstmt.close();
    }
    
    private static void displayRecords(ResultSet rs) throws SQLException {
        System.out.println("\n就医记录列表：");
        System.out.printf("%-15s %-20s %-10s %-8s %-8s\n", 
                         "记录编号", "就诊时间", "主治医生", "科室", "职称");
        System.out.println("--------------------------------------------------------");
        
        boolean hasRecords = false;
        while (rs.next()) {
            hasRecords = true;
            System.out.printf("%-15s %-20s %-10s %-8s %-8s\n",
                rs.getString("id"),
                rs.getTimestamp("create_time"),
                rs.getString("doctor_name"),
                rs.getString("department"),
                rs.getString("doctorRank")
            );
            
            System.out.println("\n诊断结果：");
            System.out.println(rs.getString("diagnosis"));
            System.out.println("\n处方信息：");
            System.out.println(rs.getString("prescription"));
            System.out.println("--------------------------------------------------------");
        }
        
        if (!hasRecords) {
            System.out.println("未找到就医记录！");
        }
    }
} 